﻿#region Copyright 2011 Mike Goatly
// This source is subject to the the MIT License (MIT)
// All rights reserved.
#endregion

namespace Lifti.Tests.Persistence.PageManagerTests.Transactional
{
    using System.Transactions;

    using Lifti.Persistence;
    using Lifti.Tests.Persistence.PageManagerTests.Setup;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Moq;

    /// <summary>
    /// Tests for creating a page within the transactional page manager.
    /// </summary>
    [TestClass]
    public class CreatingPage : TransactionalPageManagerTestBase
    {
        /// <summary>
        /// When a new page is created in the page manager, it should 
        /// be recorded in the transaction log.
        /// </summary>
        [TestMethod]
        public void ShouldRecordCreatedPageInLog()
        {
            // Setup
            IndexNodeDataPage newPage;
            using (var scope = new TransactionScope())
            {
                // Execute
                var page = (IndexNodeDataPage)this.PageManager.GetPage(0);
                newPage = this.PageManager.CreatePage(page);
            }

            this.TransactionLog.Verify(l => l.RegisterAffectedPage(newPage, PageWriteLevels.Created), Times.Exactly(1));
        }

        /// <summary>
        /// Creates the mocked data file manager.
        /// </summary>
        protected override void CreateDataFileManagerMock()
        {
            this.DataFileManager = new MockDataFileManagerSetup(false)
                .IndexNodePage(0, null, 2, new ItemRefSetup(2, 5, 88).AndNodeRef(6, 4, 'a'))
                .ItemPage(1, null, null, new IndexedItemSetup<int>(1, 3))
                .IndexNodePage(2, 0, null, new ItemRefSetup(8, 3, 12))
                .EmptyPage(3)
                .Prepare();
        }
    }
}
